// File: src/main.cpp
#include "ui/cli/CliHandler.h"
#include "ui/qt/KingdomWindow.h"
#include <iostream>

int main(int argc, char* argv[]) {
    if (argc < 2) {
    std::cout << "欢迎使用TaskMaster工具！" << std::endl;
    std::cout << "请选择启动方式：" << std::endl;
    std::cout << "1.在/build目录下执行命令./taskmaster --gui进入图形化界面" << std::endl;
    std::cout << "2.在/build目录下执行命令./taskmaster --cli进入命令行界面" << std::endl;
            return 1;
    }
    if (std::string(argv[1]) == "--cli") {
        // CLI模式
        CliHandler cli;
        cli.runInteractive("");
    } 
    else if(std::string(argv[1]) == "--gui")
    {
        // GUI模式
        // GUI 模式（弹出登录对话框）
        QApplication app(argc, argv);
        // 设置全局字体
        QFont appFont("Segoe UI", 10);
        app.setFont(appFont);
        
        XPSystem xpSystem;
        std::vector<Territory> territories;
        
        // 弹出登录对话框
        QString username, password;
        bool isRegister = false;
        if (KingdomWindow::showLoginDialog(username, password, isRegister)) {
            // 验证用户名和密码
            CliHandler cli;
            std::string user = username.toStdString();
            std::string pass = password.toStdString();
        // 自动注册逻辑
            if (isRegister && !cli.authManager.userExists(user)) 
            {
                cli.authManager.registerUser(user, pass);
                QMessageBox::information(nullptr, "注册成功", "新用户已自动注册！");
            }
            // 验证登录
            if (cli.authManager.authenticate(username.toStdString(), 
                password.toStdString())) 
            {
                /*
                QAudioDevice defaultDevice = QMediaDevices::defaultAudioInput();
                if (defaultDevice.isNull()) {
                    qCritical() << "No audio input device available";
                } else {
                    qDebug() << "Default audio input:" << defaultDevice.description();
                }
                */
                KingdomWindow window(xpSystem, territories, user);
                window.show();
                return app.exec();
            }   
            else 
            {
                QMessageBox::critical(nullptr, "错误", "用户名或密码无效！");
            }
        }
        return 0;
    }
    else {
        CliHandler cli;
        cli.handleCommand(argc, argv);
        //std::cerr << "Usage: " << argv[0] << " <username> [--cli | --gui]" << std::endl;
    }
    return 0;
}
